﻿using UnityEngine;
using UnityEngine.UI;

namespace View
{
    public class IconProxy
    {
        protected GameObject _gameObject;
        protected RectTransform _transform;
        protected Image _image;
        
        public IconProxy()
        {
            _gameObject = new GameObject("ViewProxy", typeof(RectTransform), typeof(Image));
            _transform = (RectTransform) _gameObject.transform;
            _image = _gameObject.GetComponent<Image>();
            _image.raycastTarget = false;
            _image.enabled = false;

            _transform.anchorMin = Vector2.zero;
            _transform.anchorMax = Vector2.one;
            _transform.offsetMin = Vector2.zero;
            _transform.offsetMax = Vector2.zero;
        }

        public void AddTo(Transform parent)
        {
            _transform.SetParent(parent, false);
        }

        public void EnableRaycast(bool value)
        {
            _image.raycastTarget = value;
        }
        
        public void Clear()
        {
            _image.sprite = null;
            _image.enabled = false;
        }

        public void Load(string assetName)
        {
            Texture2D texture2D = Resources.Load<Texture2D>(assetName);
            Rect rect = new Rect(0, 0, texture2D.width, texture2D.height);
            _image.sprite = Sprite.Create(texture2D, rect, new Vector2(0.5f, 0.5f));
            _image.enabled = true;
        }

        public void Dispose()
        {
            GameObject.Destroy(_gameObject);
        }
    }
}